package userdisplays;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import users.User;

@SuppressWarnings("serial")
public class UserPanel extends JPanel implements Serializable, Editable
{
    private static final Font userFont = new Font("Arial", Font.BOLD, 14);
    private static final Font subtitleFont = new Font("Arial", Font.PLAIN, 10);
    
    protected User user;
    protected transient UserPanelGroup display;

    private List<Editable> editableComponents;

    public UserPanel(User user)
    {
        super();
        this.user = user;
        editableComponents = new LinkedList<Editable>();
        
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        
        EditableImagePanel pic = new EditableImagePanel(this);
        editableComponents.add(pic);
        c.insets = new Insets(3, 3, 3, 3);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.gridheight = 3;
        add(pic, c);
        
        EditableField field;
        field = new EditableField(this, userFont, user.getName(), 100, 20);
        editableComponents.add(field);
        c.gridx = 1;
        c.gridheight = 1;
        add(field, c);
        
        field = new EditableField(this, subtitleFont, "", 100, 20);
        editableComponents.add(field);
        c.gridy = 1;
        add(field, c);

        EditableProgressBar bar = new EditableProgressBar(this, Color.RED);
        editableComponents.add(bar);
        c.gridy = 2;
        add(bar, c);
        
        /*bar = new EditableProgressBar(this, Color.BLUE);
        editableComponents.add(bar);
        c.gridy = 3;
        add(bar, c);*/
        
        EditableArea a = new EditableArea(this, "");
        editableComponents.add(a);
        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 2;
        add(new JScrollPane(a,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED), c);
        
        /*pane.add(button, c);

        button = new JButton("Button 2");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 0;
        pane.add(button, c);

        button = new JButton("Button 3");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 0;
        pane.add(button, c);

        button = new JButton("Long-Named Button 4");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 40;      //make this component tall
        c.weightx = 0.0;
        c.gridwidth = 3;
        c.gridx = 0;
        c.gridy = 1;
        pane.add(button, c);

        button = new JButton("5");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;       //reset to default
        c.weighty = 1.0;   //request any extra vertical space
        c.anchor = GridBagConstraints.PAGE_END; //bottom of space
        c.insets = new Insets(10,0,0,0);  //top padding
        c.gridx = 1;       //aligned with button 2
        c.gridwidth = 2;   //2 columns wide
        c.gridy = 2;       //third row
        pane.add(button, c);*/
    }

    protected void setUserPanelGroup(UserPanelGroup group)
    {
        display = group;
    }

    protected void sendPanel()
    {
        display.getModuleGroup().sendUpdate(new UserPanelUpdate(user, this));
    }

    public void lock()
    {
        for (Editable e : editableComponents)
            e.lock();
    }

    public void unlock()
    {
        for (Editable e : editableComponents)
            e.unlock();
    }
}
